package com.angel.provider.sys.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.base.BaseProviderImpl;
import com.angel.core.util.StringUtils;
import com.angel.dao.health.SysAreaMapper;
import com.angel.health.SysArea;
import com.angel.health.SysAreaExample;
import com.angel.health.SysAreaExample.Criteria;
import com.angel.health.SysAreaExt;
import com.angel.health.SysMenuExt;
import com.angel.provider.sys.SysManagerAreaProvider;

/**
 * 高亚林
 * 2016年9月22日下午1:15:21
 */

@Service
public class SysManagerAreaProviderImpl extends BaseProviderImpl<SysArea> implements SysManagerAreaProvider {

	@Autowired
	private SysAreaMapper mapper;
	
	//查询所有区域
	public List<SysArea> findAllArea(SysArea area) {
		return mapper.findAllArea(area);
	}

	//批量删除
	public int delete(String ids) {
		try {
			String[] idarr = ids.split("&");
			for (String id : idarr) {
				SysArea area = new SysArea();
				area.setId(Integer.parseInt(id));
				area.setDelFlag("0");
				mapper.updateByPrimaryKeySelective(area);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	//删除单个
	public int deleteById(SysArea area) {
		area.setDelFlag("0");
		int i = mapper.updateByPrimaryKeySelective(area);
		return i;
	}

	//保存
	public Map<String, String> save(SysArea area) {
		Map<String, String> map = new HashMap<String, String>();
		if(area == null || area.getName() == null || area.getName() == ""){
			map.put("msg", "名称不能为空");
			map.put("code", "0");
			return map;
		}
		SysArea sysArea = new SysArea();
		sysArea.setName(area.getName());
		List<Map<String, Object>> list = mapper.findAll(sysArea);
		if(list != null && list.size() > 0){
			map.put("msg", "区域已存在");
			map.put("code", "0");
			return map;
		}
		area.setCreateTime(new Date());
		area.setDelFlag("1");
		String code;
		Integer codeInt;
		String proCode;
		String citCode;
		if(area.getParentId() == null){
			if("3".equals(area.getType()) || "4".equals(area.getType())){
				map.put("msg", "请先选择上级区域");
				map.put("code", "0");
				return map;
			}
		}
		//获取区域类型
		String type = area.getType();
		//如果为省，获取数据库中省份code最大的省份
		SysArea area1 = new SysArea();
		if("2".equals(type)){
			area1.setType("2");
			List<Map<String, Object>> list2 = mapper.findAll(area1);
			//如果数据库中没有省份，则将编号定位001000
			if(list2 == null || list2.size() < 1){
				area.setCode("001000");
			}else{
				//如果有，获取编号最大的省份，然后截取前三位+1
				code = (String) list2.get(list2.size()-1).get("code");
				codeInt = Integer.parseInt(code)+1000;
				code = code.substring(0, code.length() - codeInt.toString().length()) + codeInt;
				area.setCode(code);
			}
		}
		//如果为市，获取数据库中市code最大的市
		if("3".equals(type)){
			//获取父节点，如果不存在，提示先输入省份
			area1.setId(area.getParentId());
			List<Map<String, Object>> list3 = mapper.findAll(area1);
			if(list3 == null || list3.size() < 1){
				map.put("msg", "请先添加对应的省份");
				map.put("code", "0");
				return map;
			}
			//获取同一省份下的编号最大的市
			SysArea area2 = new SysArea();
			area2.setParentId(area.getParentId());
			List<Map<String, Object>> list4 = mapper.findAll(area2);
			//如果不存在，则获取省份后三位+1
			if(list4 == null || list4.size() < 1){
				code = (String) list3.get(0).get("code");
				proCode = code.substring(0,3);
				code = proCode + "001";
				area.setCode(code);
			}else{
				//如果存在，获取最大编号+1
				code = (String) list4.get(list4.size()-1).get("code");
				proCode = code.substring(0,3);
				citCode = code.substring(3);
				codeInt = Integer.parseInt(citCode)+1;
				code = proCode + citCode.substring(0,citCode.length() - codeInt.toString().length()) + codeInt;
				area.setCode(code);
			}
		}
		mapper.insert(area);
		//设置pull_path
		if(area.getParentId() == null){
			//如果没有父节点，则全路径为id
			area.setFullPath(area.getId().toString());
		}else{
			//如果有父节点，全路径为父节点的全路径拼接+"_"+本节点的id
			SysArea area2 = mapper.selectByPrimaryKey(area.getParentId());
			area.setFullPath(area2.getFullPath() + "_" +area.getId());
		}
		mapper.updateByPrimaryKeySelective(area);
		map.put("msg", "添加成功");
		map.put("code", "1");
		return map;
	}

	//根据id查询
	public SysAreaExt findUserById(SysArea area) {
		List<SysAreaExt> list = mapper.findUserById(area);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	//修改
	public Map<String, String> update(SysArea area) {
		Map<String, String> map = new HashMap<String, String>();
		if(area == null || area.getName() == null || area.getName() == ""){
			map.put("msg", "名称不能为空");
			map.put("code", "0");
			return map;
		}
		area.setUpdateTime(new Date());
		mapper.updateByPrimaryKeySelective(area);
		map.put("msg", "修改成功");
		map.put("code", "1");
		return map;
	}

	@Override
	public List<SysArea> findByPid(SysArea area) {
		// TODO Auto-generated method stub
		return mapper.findByPid(area);
	}

}
